<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加分类</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="__LAYUI__/css/layui.css" rel="stylesheet">
    <style>
        .container {
            display: flex;
            justify-content: start;
        }

        .image-box {
            width: 100px;
            height: 100px;
            border: 2px dashed #ccc;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2em;
            color: #ccc;
            cursor: pointer;
            position: relative;
            margin-right: 20px;
        }

        .image-box img {
            max-width: 100%;
            max-height: 100%;
        }

        .image-box .delete-btn {
            position: absolute;
            top: 0;
            right: 0;
            background-color: red;
            color: white;
            border: none;
            padding: 2px;
            cursor: pointer;
            display: none;
        }

        /* 搜索区域样式优化 */
        .layui-inline {
            margin-bottom: 10px;
            margin-right: 10px;
        }

        /* 响应式布局 */
        @media screen and (max-width: 768px) {
            #pagination {
                flex-wrap: wrap;
            }

            .user-meta {
                flex-direction: column;
            }

            .user-meta span {
                margin-bottom: 3px;
            }
        }
    </style>
</head>

<body>
    <div class="layui-fluid">
        <form class="layui-form  layui-padding-3" action="" style="background-color: #fff;">
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-block">
                    <input type="text" name="cate_name" lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">父分类选择</label>
                <div class="layui-input-inline">
                    <select name="pid" class="layui-select" id="" lay-filter="pid">
                        <option value="0" selected>新建父分类</option>
                        {foreach $cate as $k => $v}
                        <option value="{$v.id}">{$v.cate_name}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否显示</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" lay-skin="switch" lay-filter="switchTest" title="是|否"
                        checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">私密板块<br>(登录才能查看)</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="is_hide" lay-skin="switch" lay-filter="switchTest" title="开启|关闭">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类权重</label>
                <div class="layui-input-block">
                    <input type="number" name="sort" lay-verify="required|number" placeholder="请输入分类权重" autocomplete="off"
                        class="layui-input" value="0">
                </div>
            </div>
            <div class="layui-form-item" id="icon">
                <label class="layui-form-label">Icon</label>
                <div class="container">
                    <div class="image-box" data-index="0">
                        {if @$data.icon != ''}
                        <img src="{$data.icon}" alt="图片1">
                        <button class="delete-btn" style="display: inline-block;">×</button>
                        {else}
                        +
                        {/if}
                    </div>
                </div>
                <input type="file" id="fileInput" style="display: none;">
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即添加</button>
                </div>
            </div>
        </form>
    </div>


    <script src="__LAYUI__/layui.js"></script>
    <script>
        layui.use(function () {
            var form = layui.form;
            var layer = layui.layer;
            var util = layui.util;
            $ = layui.$;

      

            // 提交事件
            form.on('submit(demo1)', function (data) {
                var field = data.field;
                img = $('.image-box').children('img').attr('src')
                field.icon = img
                field.status = field.status == 'on' ? 1 : 0
                field.is_hide = field.is_hide == 'on' ? 1 : 0

                $.post('/admin/category/doAdd', field, (res) => {
                    if (res.code == 200) {
                        layer.msg('添加成功')
                    } else {
                        layer.msg(res.msg)
                    }
                })
                return false;
            });

            // 上传图片
            const uploadUrl = '/admin/category/uploadIcon';
            $('.image-box').click(function () {
                th = $(this)
                if ($(this).text().trim() === '+') {
                    $('#fileInput').click();
                    $('#fileInput').off('change').on('change', function (event) {
                        const file = event.target.files[0];
                        if (file) {
                            const formData = new FormData();
                            formData.append('icon', file);

                            $.ajax({
                                url: uploadUrl,
                                type: 'POST',
                                data: formData,
                                contentType: false,
                                processData: false,
                                success: function (res) {
                                    if (res.code == 200 && res.data) {
                                        // 修复图片显示问题
                                        th.html(`
                                            <img src="${res.data}" alt="Uploaded Image">
                                            <button class="delete-btn">&times;</button>
                                        `);
                                        th.find('.delete-btn').show();
                                    } else {
                                        layer.msg(res.msg)
                                    }
                                },
                                error: function () {
                                    layer.msg('上传失败')
                                }
                            });
                        }
                    });
                }
            });

            $(document).on('click', '.delete-btn', function (e) {
                e.stopPropagation(); // 阻止事件冒泡到image-box
                $(this).parent().html('+');
            });
        });
    </script>
</body>

</html>